package org.nokatag.tagjava;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.nokatag.password.AESPlus;
import org.nokatag.system.Concest;
/**
 * 调用服务器方法
 * @author xiefangjian
 *
 */
public class ServerFunction extends NokaTag{

	private static final long serialVersionUID = -5874903277812079540L;
	private String serverfunction=null;//服务器方法，形如:org.noka.f.Fun('a',34,4.5);
	private String webfunction = null;//客户端调用方法
	private String refunction=null;//回调方法
	private String id=null;//
	private String asynchronous = null;//是否异步提交
	private String url = null;//服务器地址
	
	public int doStartTag()throws JspException{
		JspWriter jspOut = pageContext.getOut();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(potjs(request));//加载Prototype
		//------------------------------------------------------
		stringBuilder.append(ServerCheckJS(request));
		//------------------------------------------------------
		try {
			jspOut.write(stringBuilder.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}

	/**
	 * 服务器验证方法
	 * @return
	 */
	private String ServerCheckJS(HttpServletRequest request){
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("");
		if(serverfunction!=null && serverfunction.trim().length()>0){//ServerCheck:org.nck.name(this.value,'sd','fd',34)
			String basePath = basepath(request);
			//--------------有参数-----------------------------------------------------
			if(webfunction.indexOf("(")!=-1 && webfunction.indexOf(")")!=-1){
				String[] vars=webfunction.substring(webfunction.indexOf("(")+1,webfunction.indexOf(")")).split(",");
				String[] sars=serverfunction.substring(serverfunction.indexOf("(")+1,serverfunction.indexOf(")")).split(",");
				if(vars.length!=sars.length){//参数不统一
					return stringBuilder.toString();
				}
				serverfunction=AESPlus.encrypt(serverfunction,Concest.SQLKEY);
				stringBuilder.append("<script type=\"text/javascript\" language=\"javascript\" defer=\"true\">");
				stringBuilder.append("function "+webfunction+"{ ");//webfunction:fun(a,v,b)
				stringBuilder.append("var parsv=\"fun="+serverfunction+"\"; ");//方法
				
				for(int i=0;i<vars.length;i++){
					stringBuilder.append("parsv=parsv+\"&"+sars[i].split(" ")[1]+"=\"+"+vars[i]+"; ");
				}
				stringBuilder.append("var myAjax = new Ajax.Request(");
				stringBuilder.append("{");
				stringBuilder.append(" parameters:parsv,");
				stringBuilder.append("  method:'post',"); // 使用GET方式发送HTTP请求
				if(null!=asynchronous){
					stringBuilder.append("  asynchronous:"+asynchronous+","); //是否异步提交
				}
				stringBuilder.append("  onComplete: "+refunction+" ");// 指定请求成功完成时需要执行的方法
				stringBuilder.append("}");
				stringBuilder.append(");");
				stringBuilder.append("}");
				stringBuilder.append(" </script>");
			}else{//-------------没参数----------------------------------------------------
				serverfunction=AESPlus.encrypt(serverfunction,Concest.SQLKEY);
				stringBuilder.append("<script type=\"text/javascript\" language=\"javascript\" defer=\"true\">");
				stringBuilder.append("function "+webfunction+"(){");//webfunction:fun(a,v,b)
				stringBuilder.append("var parsv=\"fun="+serverfunction+"\";");//方法
				stringBuilder.append("var myAjax = new Ajax.Request(");
				stringBuilder.append("\""+basePath+"/nokatag/ajax/inputtextcheck.serfun\", ");// 请求的URL
				stringBuilder.append("{");
				stringBuilder.append("parameters:parsv,");
				stringBuilder.append("method:'post',"); // 使用GET方式发送HTTP请求
				stringBuilder.append("onComplete: "+refunction+" ");// 指定请求成功完成时需要执行的方法
				stringBuilder.append("}");
				stringBuilder.append(");");
				stringBuilder.append("}");
				stringBuilder.append("</script>");
			}
		}else if(null!=url){
			String[] vars=webfunction.substring(webfunction.indexOf("(")+1,webfunction.indexOf(")")).split(",");
			stringBuilder.append("<script type=\"text/javascript\" language=\"javascript\" defer=\"true\">");
			stringBuilder.append("function "+webfunction+"{ ");//webfunction:fun(a,v,b)
			stringBuilder.append("var parsv=\"\"; ");//参数
			for(int i=0;i<vars.length;i++){
				stringBuilder.append("parsv=parsv+\"&"+vars[i]+"=\"+"+vars[i]+"; ");
			}
			stringBuilder.append("var myAjax = new Ajax.Request(");
			stringBuilder.append("\""+url+"\",");// 请求的URL
			stringBuilder.append("{");
			stringBuilder.append(" parameters:parsv,");
			stringBuilder.append("  method:'post',"); // 使用GET方式发送HTTP请求
			if(null!=asynchronous){
				stringBuilder.append("  asynchronous:"+asynchronous+","); //是否异步提交
			}
			stringBuilder.append("  onComplete: "+refunction+" ");// 指定请求成功完成时需要执行的方法
			stringBuilder.append("}");
			stringBuilder.append(");");
			stringBuilder.append("}");
			stringBuilder.append(" </script>");
		}
		return stringBuilder.toString();
	}
	public String getServerfunction() {
		return serverfunction;
	}

	public void setServerfunction(String serverfunction) {
		this.serverfunction = serverfunction;
	}

	public String getWebfunction() {
		return webfunction;
	}

	public void setWebfunction(String webfunction) {
		this.webfunction = webfunction;
	}

	public String getRefunction() {
		return refunction;
	}

	public void setRefunction(String refunction) {
		this.refunction = refunction;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getAsynchronous() {
		return asynchronous;
	}

	public void setAsynchronous(String asynchronous) {
		this.asynchronous = asynchronous;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
}
